package erfgame.frontend.desktop.view.terrain.iso.handler;

import java.awt.event.MouseEvent;

public abstract class BaseClickInteractionHandler extends BaseInteractionHandler {

	private int buttonId;
	private MouseEvent mouseDownEvent;
	
	public BaseClickInteractionHandler( int buttonId ) {
		this.buttonId = buttonId;
	}
	
	@Override
	public MouseEvent[] handleMouseEvent(MouseEvent e) {
		MouseEvent[] result;
		switch( e.getID() ) {
		case MouseEvent.MOUSE_PRESSED:
			if( e.getButton() == this.buttonId ) {
				this.mouseDownEvent = e;
				result = null;
			} else {
				result = new MouseEvent[]{e};
			}
		case MouseEvent.MOUSE_RELEASED:
			if( e.getButton() == this.buttonId && this.mouseDownEvent != null ) {
				boolean consumed;
				consumed = this.handleClick(this.mouseDownEvent);
				if( consumed ) {
					result = null;
				} else {
					result = new MouseEvent[]{ this.mouseDownEvent, e };
				}
				this.mouseDownEvent = null;
			} else {
				result = new MouseEvent[]{e};
			}
			break;
		case MouseEvent.MOUSE_DRAGGED:
			if( this.mouseDownEvent != null ) {
				result = new MouseEvent[]{ this.mouseDownEvent, e };
				this.mouseDownEvent = null;
				break;
			}
		default:
			result = new MouseEvent[]{e};
		}
		return result;
	}
	
	
	protected abstract boolean handleClick( MouseEvent e );
}
